package com.yangjun.service.departmentCustomization;

import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.yangjun.common.DVConstants;
import com.yangjun.entity.departmentCustomizationAnalysis.PreSalesSectionOverViewEntity;
import com.yangjun.utils.DvUtils;
/**
 * 
 * @ClassName PreSalesPharmacistService
 * @Description 可视化部门定制售前科组重点产品页面的内容
 * @author Administrator
 * @Date 2017年3月15日 上午10:35:01
 * @version 1.0.0
 */
@Service
public class PreSalesKeyProductService extends DepartmentCustomizationCommonService{
	/**
	 * @Description 重点产品的排行
	 * @param groupName
	 * @param wareName
	 * @return
	 * @author yangjun
	 * @Date 2017年3月21日 下午3:47:22
	 */
	//任务完成率排名
	public List<Map<String,Object>> queryKeyProductTaskRank(String groupName){
		List<PreSalesSectionOverViewEntity> keyProductRankEntityList = this.preSalesSectionMapper.queryKeyProductTaskRank(groupName);
		//组装数据
		List<Map<String,Object>> rankList = rankPreSalesSectionOverViewEntityList(keyProductRankEntityList);
		//排序
		sortRankPreSalesSectionOverViewEntityList(rankList,new FloatComparatorSortList("value"));
		return rankList;
	}
	//客服销售排行榜
	public List<Map<String,Object>> queryKeyProductStaffSalesRank(String groupName,String wareName){
		List<PreSalesSectionOverViewEntity> staffRankEntityList = this.preSalesSectionMapper.queryKeyProductStaffSalesTaskRank(groupName,wareName);
		//组装数据
		List<Map<String,Object>> rankList = rankPreSalesSectionOverViewEntityList(staffRankEntityList);
		//排序
		sortRankPreSalesSectionOverViewEntityList(rankList,new FloatComparatorSortList("value"));
		return rankList;
	}
	/**
	 * @Description 销售指标表格
	 * @param groupName
	 * @param sourceName
	 * @param staffName
	 * @param wareName
	 * @return
	 * @author yangjun
	 * @Date 2017年3月21日 下午3:47:52
	 */
	public List<Object[]> queryKeyProductStaffSalesTable(String groupName,String sourceName,String staffName,String wareName){
		List<Object[]> resultList = Lists.newArrayList();
		List<PreSalesSectionOverViewEntity> preSalesSectionOverViewEntityList = this.preSalesSectionMapper.queryKeyProductStaffSalesTable(groupName,sourceName,staffName,wareName);
		if(preSalesSectionOverViewEntityList.size()>0){
			for(PreSalesSectionOverViewEntity preSalesSectionOverViewEntity : preSalesSectionOverViewEntityList){
				String name = preSalesSectionOverViewEntity.getStaffName();
				String keyProductName = preSalesSectionOverViewEntity.getKeyProduct();
				int adviceCnt= preSalesSectionOverViewEntity.getAdviceCnt();
				int validAdviceCnt= preSalesSectionOverViewEntity.getValidAdviceCnt();
				int orderCnt = preSalesSectionOverViewEntity.getOrderCnt();
				int orderQty = preSalesSectionOverViewEntity.getOrderQty();
				float salesOrderAmount = preSalesSectionOverViewEntity.getSalesOrderAmount();
				float salePct = preSalesSectionOverViewEntity.getSalePct();
				int nopayOrderCnt = preSalesSectionOverViewEntity.getNopayOrderCnt();
				float nopayOrderAmount = preSalesSectionOverViewEntity.getNopayOrderAmount();
				int totalTask = preSalesSectionOverViewEntity.getTotalTask();
				float doneRate = preSalesSectionOverViewEntity.getDoneRate();
				int workDays = preSalesSectionOverViewEntity.getWorkDays();
				int tslPerDay = preSalesSectionOverViewEntity.getTskPerDay();
				Object[] dataArr = new Object[]{name,keyProductName,adviceCnt,validAdviceCnt,orderCnt,orderQty,salesOrderAmount,salePct,nopayOrderCnt,nopayOrderAmount,totalTask,doneRate,workDays,tslPerDay};
				if(arrIsExistNull(2,dataArr)>0){
					resultList.add(dataArr);
				}
			}
		}
		DvUtils.PinYinComparatorSortList(resultList);
		return resultList;
	}
	/**
	 * @Description 到渠道维度的销售指标表格
	 * @param groupName
	 * @param sourceName
	 * @param staffName
	 * @param wareName
	 * @return
	 * @author yangjun
	 * @Date 2017年3月21日 下午3:48:07
	 */
	public List<Object[]> queryKeyProductStaffSalesTableByChannel(String groupName,String sourceName,String staffName,String wareName){
		List<Object[]> resultList = Lists.newArrayList();
		List<PreSalesSectionOverViewEntity> preSalesSectionOverViewEntityList = this.preSalesSectionMapper.queryKeyProductStaffSalesTableByChannel(groupName,sourceName,staffName,wareName);
		if(preSalesSectionOverViewEntityList.size()>0){
			for(PreSalesSectionOverViewEntity preSalesSectionOverViewEntity : preSalesSectionOverViewEntityList){
				String channelName = preSalesSectionOverViewEntity.getChannel();
				int adviceCnt= preSalesSectionOverViewEntity.getAdviceCnt();
				int validAdviceCnt= preSalesSectionOverViewEntity.getValidAdviceCnt();
				int orderCnt = preSalesSectionOverViewEntity.getOrderCnt();
				float salesOrderAmount = preSalesSectionOverViewEntity.getSalesOrderAmount();
				float salePct = preSalesSectionOverViewEntity.getSalePct();
				int nopayOrderCnt = preSalesSectionOverViewEntity.getNopayOrderCnt();
				float nopayOrderAmount = preSalesSectionOverViewEntity.getNopayOrderAmount();
				Object[] dataArr = new Object[]{channelName,adviceCnt,validAdviceCnt,orderCnt,salesOrderAmount,salePct,nopayOrderCnt,nopayOrderAmount};
				//判断数组是不是全部为0，是就过滤不是就添加返回
				if(arrIsExistNull(1,dataArr)>0){
					resultList.add(dataArr);
				}
			}
		}
		return resultList;
	}
	/**
	 * @Description 趋势对比日月对比
	 * @param groupName
	 * @param channelName
	 * @param roomName
	 * @return
	 * @author yangjun
	 * @Date 2017年3月10日 下午2:42:01
	 */
	public Map<String,Object> queryKeyProductComparisonLineChartByDayofMonth(String groupName,String sourceName,String staffName,String wareName){
		String currentDate = sdfMonth.format(new Date());
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.MONTH, -1);
		String contrastDate = sdfMonth.format(calendar.getTime());
		String[] dateArr = {currentDate,contrastDate};
		//一次性查询两天的数据
		List<PreSalesSectionOverViewEntity> LineChartList = this.preSalesSectionMapper.queryKeyProductComparisonLineChartByDay(groupName,sourceName,staffName,wareName,dateArr);
		int index = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
		return queryLineChartData(DVConstants.MONTH_DAY,LineChartList,dateArr,DVConstants.COMPARISON_LEGEND,index);
	}
	
	/**
	 * @Description 本周上周对比的折线图
	 * @param groupName
	 * @param sourceName
	 * @param staffName
	 * @param wareName
	 * @return
	 * @author yangjun
	 * @Date 2017年3月21日 下午3:46:05
	 */
	public Map<String,Object> queryKeyProductComparisonLineChartByWeek(String groupName,String sourceName,String staffName,String wareName){
		Calendar calendar = Calendar.getInstance();
		String currentDate = sdfDay.format(new Date());
		calendar.setTime(new Date());
		int dayOfWeek= calendar.get(Calendar.DAY_OF_WEEK)-1;
		calendar.add(Calendar.DAY_OF_MONTH, -(dayOfWeek+6));
		String contrastDate = sdfDay.format(calendar.getTime());
		calendar.add(Calendar.DAY_OF_MONTH, +6);
		Date tmpDate = calendar.getTime();
		//一次性查询两天的数据
		List<PreSalesSectionOverViewEntity> LineChartList = setDayOfWeek(preSalesSectionMapper.queryKeyProductComparisonLineChartByWeek(groupName,sourceName,staffName,wareName,currentDate,contrastDate),tmpDate);
		int index = Calendar.getInstance().get(Calendar.DAY_OF_WEEK)-1;
		return queryLineChartData(DVConstants.WEEK_NAME_ARR,LineChartList,new String[]{"本周","上周"},DVConstants.COMPARISON_LEGEND,index);
	}
	/**
	 * @Description 近12个月的数据表格
	 * @param groupName
	 * @param sourceName
	 * @param staffName
	 * @param wareName
	 * @return
	 * @author yangjun
	 * @Date 2017年3月21日 下午3:46:05
	 */
	public Map<String,List<String[]>> queryKeyProductTableDataByMonth(String groupName,String sourceName,String staffName,String wareName){
		Date today = new Date();
		String endDate = sdfDay.format(today);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(today);
		calendar.add(Calendar.YEAR, -1);
		calendar.set(Calendar.DAY_OF_MONTH,1);
		String beginDate = sdfDay.format(calendar.getTime());
		//一次性查询12个月的数据
		List<PreSalesSectionOverViewEntity> tableByDayList = preSalesSectionMapper.queryKeyProductTableData(groupName,sourceName,staffName,wareName,beginDate,endDate,"MONTH");
		return queryTableData(tableByDayList,DVConstants.MONTH_DAY,"channel","month");
	}
	/**
	 * @Description 近两年的数据表格
	 * @param groupName
	 * @param sourceName
	 * @param staffName
	 * @param wareName
	 * @return
	 * @author yangjun
	 * @Date 2017年3月21日 下午3:46:05
	 */
	public Map<String,List<String[]>> queryKeyProductTableDataByYear(String groupName,String sourceName,String staffName,String wareName){
		Date today = new Date();
		String endDate = sdfDay.format(today);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(today);
		calendar.add(Calendar.YEAR, -1);
		calendar.set(Calendar.DAY_OF_YEAR,1);
		String beginDate = sdfDay.format(calendar.getTime());
		//一次性查询两天的数据
		List<PreSalesSectionOverViewEntity> tableByDayList = preSalesSectionMapper.queryKeyProductTableData(groupName,sourceName,staffName,wareName,beginDate,endDate,"YEAR");
		return queryTableData(tableByDayList,DVConstants.MONTH,"channel","year");
	}
}
